package com.wc.alorithm_blue_bridge._数据结构.tire.Q3755;

import java.util.*;
import java.io.*;
/**
 * @Author congege
 * @Description
 * 小蓝的神秘图书馆
 * https://www.lanqiao.cn/problems/3755/learning/?page=1&first_category_id=1
 * @Date Created in 2023/12/2-9:56
 */
public class Main {
    private static final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    private static final PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void main(String[] args) {
        String NM = nextLine();
        String[] nm = NM.split(" ");
        int n = Integer.parseInt(nm[0]);
        int m = Integer.parseInt(nm[1]);
        Tire root = new Tire();
        while(n-- > 0){
            String word = nextLine();
            root.insert(word);
        }
        while(m-- > 0){
            String prefix = nextLine();
            out.println(root.findPrefixNumber(prefix));
        }
        out.flush();
    }

    private static int nextInt(){
        int num = 0;
        try {
            num = Integer.parseInt(in.readLine());
        } catch (Exception e){
            return -1;
        }
        return num;
    }

    private static String nextLine(){
        String s = null;
        try {
            s = in.readLine();
        } catch (Exception e){
            return null;
        }
        return s;
    }
}

class Tire{
    private boolean isEnd;
    private Map<Character, Tire> children;
    private int num;

    public Tire(){
        isEnd = false;
        children = new HashMap<>();
        num = 0;
    }

    public void insert(String word){
        Tire node = this;
        for(char c : word.toCharArray()){
            if(!node.children.containsKey(c)){
                Tire child = new Tire();
                node.children.put(c,child);
            }
            node = node.children.get(c);
            node.num++;
        }
        node.isEnd = true;
    }

    public int findPrefixNumber(String prefix){
        Tire node = this;
        for(char c : prefix.toCharArray()){
            if(node.children.containsKey(c)){
                node = node.children.get(c);
            } else {
                return 0;
            }
        }
        return node.num;
    }
}
